<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_base.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_supplier.php');

if($_REQUEST['act'] == 'list')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类			
		
		if(!empty($_REQUEST['supplier_code']))
		{			
			$supplier_info=get_supplier_info('','',$_REQUEST['supplier_code']);
			$total_num=count($supplier_info);
		}		
		else{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$start=$num_per_page*($page-1);		
		
			$supplier_id=isset($_REQUEST['s_id'])?($_REQUEST['s_id']):0;
			$supplier_group_id=isset($_REQUEST['s_g_id'])?($_REQUEST['s_g_id']):0;
			
			$total_num=count(get_supplier_info($supplier_group_id,$supplier_id));
			
			$supplier_info=get_supplier_info($supplier_group_id,$supplier_id,'',$start,$num_per_page);	
			
			$url="erp_supplier.php?act=list&s_id=".$supplier_id;				
		}		
		
		$smarty->assign('url',  $_SERVER["REQUEST_URI"].'&page='.$page);
		
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());			
		
		$action_link = array('href' => 'erp_supplier.php?act=add_supplier', 'text' => $_LANG['erp_add_supplier']);
    $smarty->assign('action_link',  $action_link);

		$smarty->assign('supplier_info',  $supplier_info);

		$smarty->assign('admins',  get_admin());
		
		$smarty->assign('supplier_group',  get_supplier_group());
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_supplier_list']);
		
		$smarty->display('erp_supplier_list.htm');
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif($_REQUEST['act'] == 'change_admin')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{		
		include('../includes/cls_json.php');
		$json  = new JSON;
		
		if(!admin_priv('erp_sys_manage', '', false))
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_no_permit'];
			die($json->encode($result));
		}
		else{
			$supplier_id=isset($_REQUEST['supplier_id'])? intval($_REQUEST['supplier_id']) : 0;
			$admin_id=isset($_REQUEST['admin_id'])? intval($_REQUEST['admin_id']) : 0;
			
			if(empty($supplier_id) || empty($admin_id))
			{
				$result['error']=2;
				$result['message']=$_LANG['erp_wrong_parameter'];
				die($json->encode($result));				
			}
			
			$sql="update ".$ecs->table('erp_supplier')." set admin_id='".$admin_id."' where supplier_id='".$supplier_id."' limit 1";
			$db->query($sql);
			
			$result['error']=0;
			die($json->encode($result));			
		}	
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif($_REQUEST['act'] == 'change_group')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{			
		include('../includes/cls_json.php');
	  $json  = new JSON;
	
		if(!admin_priv('erp_sys_manage', '', false))
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_no_permit'];
			die($json->encode($result));
		}
	    
	    if(empty($_REQUEST['supplier_id']))
	    {
	        $result['error']=1;
	        $result['message']=$_LANG['erp_wrong_parameter'];
	        die($json->encode($result));
	    }
	    else{
	    
	        $supplier_id=intval($_REQUEST['supplier_id']);
	        $supplier_group_id =intval($_REQUEST['supplier_group_id']);  
	  
	        $sql="update ".$ecs->table('erp_supplier')." set supplier_group_id  ='".$supplier_group_id."' where supplier_id='".$supplier_id."'";
	        if($db->query($sql))
	        {
	            $result['error']=0;
	            die($json->encode($result));
	        }  		
	    }
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		  
}

elseif($_REQUEST['act'] == 'change_name')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{			
		include('../includes/cls_json.php');
	  $json  = new JSON;
	
		if(!admin_priv('erp_sys_manage', '', false))
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_no_permit'];
			die($json->encode($result));
		}
	    
	    if(empty($_REQUEST['supplier_id']))
	    {
	        $result['error']=1;
	        $result['message']=$_LANG['erp_wrong_parameter'];
	        die($json->encode($result));
	    }
	    if(empty($_REQUEST['new_name']))
	    {
	        $result['error']=2;
	        $result['message']=$_LANG['erp_supplier_name_required'];
	        die($json->encode($result));
	    }
	    else{
	    
	        $supplier_id=intval($_REQUEST['supplier_id']);
	        $new_name=trim($_REQUEST['new_name']);  
	  
	        $sql="update ".$ecs->table('erp_supplier')." set name='".$new_name."' where supplier_id='".$supplier_id."'";
	        if($db->query($sql))
	        {
	            $result['error']=0;
	            die($json->encode($result));
	        }  		
	    }
  }
 	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}


elseif($_REQUEST['act'] == 'change_address')
{
  include('../includes/cls_json.php');
	$json  = new JSON;
    
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
    
    if(empty($_REQUEST['supplier_id']))
    {
        $result['error']=1;
        $result['message']=$_LANG['erp_wrong_parameter'];
        die($json->encode($result));
    }
    else{   
        $supplier_id=intval($_REQUEST['supplier_id']);
        $new_address=trim($_REQUEST['new_address']);
 
        if(empty($new_address))
        {
            $result['error']=2;
            $result['address']=$_LANG['erp_NULL'];
            $new_address=$_LANG['erp_NULL'];
            $sql="update ".$ecs->table('erp_supplier')." set address='".$new_address."' where supplier_id='".$supplier_id."'";
            $db->query($sql);
            die($json->encode($result));
        }
        else{
            $sql="update ".$ecs->table('erp_supplier')." set address='".$new_address."' where supplier_id='".$supplier_id."'";
            if($db->query($sql))
            {
                $result['error']=0;
                die($json->encode($result));    
            }
            
        }
    }
}

elseif($_REQUEST['act'] == 'change_contact')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
    
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
    
    if(empty($_REQUEST['supplier_id']))
    {
        $result['error']=1;
        $result['message']=$_LANG['erp_wrong_parameter'];
        die($json->encode($result));
    }
    else{   
        $supplier_id=intval($_REQUEST['supplier_id']);
        $new_contact=trim($_REQUEST['new_contact']);    
        
        if(empty($new_contact))
        {
            $result['error']=2;
            $result['contact']=$_LANG['erp_NULL'];
            $new_contact=$_LANG['erp_NULL'];    
            $sql="update ".$ecs->table('erp_supplier')." set contact='".$new_contact."' where supplier_id='".$supplier_id."'";
            $db->query($sql);
            die($json->encode($result));            
        }
        else{
            $sql="update ".$ecs->table('erp_supplier')." set contact='".$new_contact."' where supplier_id='".$supplier_id."'";
            if($db->query($sql))
            {
                $result['error']=0;
                die($json->encode($result));    
            }   
        }
    }
}

elseif($_REQUEST['act'] == 'change_tel')
{
  include('../includes/cls_json.php');
  $json  = new JSON;
    
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
    
    if(empty($_REQUEST['supplier_id']))
    {
        $result['error']=1;
        $result['message']=$_LANG['erp_wrong_parameter'];
        die($json->encode($result));
    }
    else{   
    
        $supplier_id=intval($_REQUEST['supplier_id']);
        $new_tel=trim($_REQUEST['new_tel']);    
        if(empty($new_tel))
        {
            $result['error']=2;
            $result['tel']=$_LANG['erp_NULL'];
            
            $new_tel=$_LANG['erp_NULL'];  
            $sql="update ".$ecs->table('erp_supplier')." set tel='".$new_tel."' where supplier_id='".$supplier_id."'";
            $db->query($sql);
            die($json->encode($result));            
        }
        else{
            $sql="update ".$ecs->table('erp_supplier')." set tel='".$new_tel."' where supplier_id='".$supplier_id."'";
            if($db->query($sql))
            {
                $result['error']=0;
                die($json->encode($result));
            }   
        }
    }
}

elseif($_REQUEST['act'] == 'change_fax')
{
  include('../includes/cls_json.php');
  $json  = new JSON;
    
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
    
    if(empty($_REQUEST['supplier_id']))
    {
        $result['error']=1;
        $result['message']=$_LANG['erp_wrong_parameter'];
        die($json->encode($result));
    }
    else{   
    
        $supplier_id=intval($_REQUEST['supplier_id']);
        $new_fax=trim($_REQUEST['new_fax']);    
        if(empty($new_fax))
        {
            $result['error']=2;
            $result['fax']=$_LANG['erp_NULL'];
            
            $new_fax=$_LANG['erp_NULL'];    
            $sql="update ".$ecs->table('erp_supplier')." set fax='".$new_fax."' where supplier_id='".$supplier_id."'";
            $db->query($sql);
            die($json->encode($result));            
        }
        else{
            $sql="update ".$ecs->table('erp_supplier')." set fax='".$new_fax."' where supplier_id='".$supplier_id."'";
            if($db->query($sql))
            {
                $result['error']=0;
                die($json->encode($result));
            }             
        }
    }
}

elseif($_REQUEST['act'] == 'change_is_valid')
{
  include('../includes/cls_json.php');
  $json  = new JSON;
    
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
    
    if(empty($_REQUEST['supplier_id']))
    {
        $result['error']=1;
        $result['message']=$_LANG['erp_wrong_parameter'];
        die($json->encode($result));
    }
    else{   
        $supplier_id=intval($_REQUEST['supplier_id']);
        $sql="select is_valid from ".$ecs->table('erp_supplier')." where supplier_id='".$supplier_id."'";
        $is_valid=$db->getOne($sql);
        
        if($is_valid==1)
        {
            $result['error']=0;
            $result['is_valid']=0;
            $sql="update ".$ecs->table('erp_supplier')." set is_valid='0' where supplier_id='".$supplier_id."'";
            $db->query($sql);
        }
        else{
            $result['error']=0;
            $result['is_valid']=1;           
            $sql="update ".$ecs->table('erp_supplier')." set is_valid='1' where supplier_id='".$supplier_id."'";
            $db->query($sql);
        }

        die($json->encode($result));        
    }
}

elseif($_REQUEST['act'] == 'delete_supplier')
{
  include('../includes/cls_json.php');
  $json  = new JSON;
    
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
    
    if(empty($_REQUEST['supplier_id']))
    {
        $result['error']=1;
        $result['message']=$_LANG['erp_wrong_parameter'];
        die($json->encode($result));
    }
    else{   
        $supplier_id=intval($_REQUEST['supplier_id']);
        $supplier_info=is_supplier_exist($supplier_id);
        if($supplier_info!==false)
        {
        	$sql="select count(*) as order_number from ".$ecs->table('erp_order')." where supplier_id='".$supplier_id."'";
        	$order_number=$db->getOne($sql);
  				
            //只能删除引用数为0的供应商
            if($order_number>0)
            {
                $result['error']=2;
                $result['message']=$_LANG['erp_supplier_has_order'];
                die($json->encode($result));
            }
            else{
                $sql="delete from ".$ecs->table('erp_supplier')." where supplier_id='".$supplier_id."'";
                if($db->query($sql))
                {
                    $result['error']=0;
                    die($json->encode($result));
                }               
            }   
        }
        else{
            $result['error']=3;
            $result['message']=$_LANG['erp_supplier_not_exist'];
            die($json->encode($result));            
        }
    }
}


elseif($_REQUEST['act'] == 'edit_supplier')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$smarty->assign('act',  'edit');
		$supplier_id=$_REQUEST['supplier_id'];
		$supplier_info=get_supplier_info('',$supplier_id);	
		
		$smarty->assign('supplier_group',  get_supplier_group());
		
		$smarty->assign('supplier_info',  $supplier_info[0]);
		
		$smarty->assign('admins',  get_admin());
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_edit_supplier']);
		
		$smarty->display('erp_supplier.htm');
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}

elseif($_REQUEST['act'] == 'add_supplier')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$smarty->assign('act',  'add');
		$smarty->assign('supplier_group',  get_supplier_group());
		$supplier_code=gen_supplier_code();
		$smarty->assign('supplier_code',  $supplier_code);
		$smarty->assign('admins',  get_admin());
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_add_supplier']);
		$smarty->display('erp_supplier.htm');
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}


elseif($_REQUEST['act'] == 'save_supplier')
{
    include('../includes/cls_json.php');
    $_POST['new_supplier'] = json_str_iconv($_POST['new_supplier']);
    $json  = new JSON;
    
	  if(!admin_priv('erp_sys_manage', '', false))
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_no_permit'];
			die($json->encode($result));
		}    

    $result = array('error' => 0, 'message' => '');
    
    $supplier_info=$json->decode($_POST['new_supplier']);

    if($supplier_info->supplier_id=='EMPTY')
    {
    	$sql="insert into ".$ecs->table('erp_supplier')." set code='".$supplier_info->code."'";
    }
    else{
    	$sql="update ".$ecs->table('erp_supplier')." set code='".$supplier_info->code."'";
    }
    
    
    $sql.=", name='".$supplier_info->name."'";
		
		if(!empty($supplier_info->address))
		{
			$sql.=", address='".$supplier_info->address."'";
		}
	    else{
	    	$sql.=", address='EMPTY'";
	    }
		if(!empty($supplier_info->contact))
		{
			$sql.=", contact='".$supplier_info->contact."'";
		}
	    else{
	    	$sql.=", contact='EMPTY'";
	    }	
		if(!empty($supplier_info->tel))
		{
			$sql.=", tel='".$supplier_info->tel."'";
		}
	    else{
	    	$sql.=", tel='EMPTY'";
	    }	
		if(!empty($supplier_info->fax))
		{
			$sql.=", fax='".$supplier_info->fax."'";
		}
	    else{
	    	$sql.=", fax='EMPTY'";
	    }		
	
	  $sql.=", is_valid ='".$supplier_info->is_valid."'";
		$sql.=", supplier_group_id ='".$supplier_info->supplier_group_id."'";
		$sql.=", admin_id ='".$supplier_info->admin_id."'";
	
	  if($supplier_info->supplier_id!=='EMPTY')
	  {
	    	$sql.=" where supplier_id='".$supplier_info->supplier_id."'";
	   }	
		
	    $db->query($sql);
	    
	    die($json->encode($result));
}
?>